{% extends "personal_center/base.html" %}
<!--固定资产付款创建页-->
{% block page_link %}
    <link rel="stylesheet" href="../../static/system_setup/css/style.css">
    <link rel="stylesheet" href="../../static/personnel/TimeSelector/skin/jedate.css">
{% endblock %}


{% block style %}
    <style>
        .add_class {
            background: #1E90FF;
        }

        tr td {
            height: 37px;
            outline: none;
        }

        th {
            text-align: center;
        }

        .file {
            position: relative;
            display: inline-block;
            background: #D0EEFF;
            border: 1px solid #99D3F5;
            border-radius: 4px;
            padding: 4px 12px;
            overflow: hidden;
            color: #1E88C7;
            text-decoration: none;
            text-indent: 0;
            line-height: 20px;
        }

        .file input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
        }

        .file:hover {
            background: #AADFFD;
            border-color: #78C3F3;
            color: #004974;
            text-decoration: none;
        }

        .jeinput_1{
            display: inline-block;
            width: 100%;
            height: 36px;
            line-height: 34px\9;
            background-color: #fff;
            border: none;
        }
    </style>
{% endblock %}


{% block right_content %}
    <div class="headline">
        <div style="width: 15%;height: 100%;line-height: 40px;float: left;padding-left: 1%;">
            <a href="#" style="color: #0b0b0b;">
                <i class="iconfont icon-faqi"></i>
                <span> 新增申请</span>
            </a>
        </div>

        <div style="width: 7%;text-align: center;height: 100%;line-height: 40px;float: right;">
            <button id="add_station" type="button"
                    style="width: 100px;height: 30px;margin-top: 5px;border-radius: 3px;border: #1E90FF solid 1px;background-color: #1E90FF;color: white;line-height: 30px;"
                    onclick="save_data()">
                保&nbsp;&nbsp;存
            </button>
        </div>

        <div style="width: 7%;text-align: center;height: 100%;line-height: 40px;float: right;">
            <button id="add_station" type="button"
                    style="width: 100px;height: 30px;margin-top: 5px;border-radius: 3px;border: #1E90FF solid 1px;background-color: #1E90FF;color: white;line-height: 30px;"
                    onclick="ExportCar()">
                提&nbsp;&nbsp;交
            </button>
        </div>
    </div>
    <hr>

    <div style="width: 100%;height: 95%;overflow: auto;">
        <div id="menu_from"
             style="width: 100%;height: 100%;color: #2C3E50;display: inline-block;vertical-align: top;background: white;">
            <div style="width: 95%;height: auto;display: inline-block;vertical-align: center;margin-left: 2.5%;">
                <div style="width: 100%;">
                    <table style="width: 100%; margin-top: 1%;">
                        <tr style="width: 100%;">
                            <td colspan="4" style="height: 40px;text-align: center;border: #cfcfcf solid 0.5px;"><h3>请&nbsp;假&nbsp;申&nbsp;请</h3></td>
                        </tr>
                        <tr>
                            <td colspan="4"
                                style="border: #cfcfcf solid 0.5px;width: 95%;margin-left: 2.5%;margin-top: 1%;background: white;height: 40px;padding-left: 0.5%;line-height: 40px;background: #dfdfdf;">
                                基本信息
                            </td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;text-align: right;height: 37px;line-height: 37px;padding-right: 1%;">
                                申&nbsp;&nbsp;请&nbsp;人
                            </td>
                            <td id="applicat"
                                style="width: 35%;height: 37px;line-height: 37px;padding-left: 1%;border: #cfcfcf solid 0.5px;">
                                {{ realname }}
                            </td>
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">
                                工&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号
                            </td>
                            <td id="company"
                                style="width: 35%;height: 37px;line-height: 37px;padding-left: 1%;border: #cfcfcf solid 0.5px;">{{ obj.job_num }}</td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;height: 37px;line-height: 37px;padding-right: 1%;border: #cfcfcf solid 0.5px; text-align: right">所属科室</td>
                            <td id="company" style="width: 35%;height: 37px;line-height: 37px;padding-left: 1%;border: #cfcfcf solid 0.5px;">{{ department }}</td>
                            <td style="width: 15%;height: 37px;line-height: 37px;padding-right: 1%;border: #cfcfcf solid 0.5px; text-align: right">请假类型</td>
                            <td style="width: 35%;height: 37px;line-height: 37px;padding-left: 1%;border: #cfcfcf solid 0.5px;">
                                <select name="" id="leave_type" style="width: 100%;height: 100%;border: none;">
                                    {% for leave in leave_li %}
                                        {% if leave.value == obj.leave_type %}
                                            <option value="{{ leave.value }}" selected>{{ leave.leave }}</option>
                                        {% else %}
                                            <option value="{{ leave.value }}">{{ leave.leave }}</option>
                                        {% endif %}

                                    {% endfor %}
                                </select>
                            </td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;border-right: #cfcfcf solid 0.5px;border-left: #cfcfcf solid 0.5px;border-bottom: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">开始时间</td>
                            <td style="width: 35%;text-align: left;padding-left: 1%;border-bottom: #cfcfcf solid 0.5px;" id="start_time">
                                <input type="text" class="jeinput_1" id="test12" placeholder="选择开始日期" value="{{ obj.start_time|date:'Y-m-d' }}" autocomplete="off"></td>
                            <td style="width: 15%;border-right: #cfcfcf solid 0.5px;border-left: #cfcfcf solid 0.5px;border-bottom: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">结束时间</td>
                            <td style="width: 35%;border-right: #cfcfcf solid 0.5px; text-align: left;padding-left: 1%;border-bottom: #cfcfcf solid 0.5px;" id="end_time">
                                <input type="text" class="jeinput_1" id="test13" placeholder="选择结束日期" value="{{ obj.end_time|date:'Y-m-d' }}" autocomplete="off">
                            </td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;border-right: #cfcfcf solid 0.5px;border-left: #cfcfcf solid 0.5px;border-bottom: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">
                                请假事由
                            </td>
                            <td style="width: 35%;border-right: #cfcfcf solid 0.5px;border-left: #cfcfcf solid 0.5px;border-bottom: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;padding-left: 1%;">
                                <input type="text" id="reason" style="width: 100%; height: 100%; border: none;" value="{{ obj.reason }}">
                            </td>
                            <td style="width: 15%;border-right: #cfcfcf solid 0.5px;border-left: #cfcfcf solid 0.5px;border-bottom: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">人员类别</td>
                            <td style="width: 35%;border-right: #cfcfcf solid 0.5px; text-align: left;padding-left: 1%;border-bottom: #cfcfcf solid 0.5px;">
                                <input type="radio" name="people_type" value="1" {% if obj.people_type == '1' %}checked{% endif %}> 行管
                                <input type="radio" name="people_type" value="2" style="margin-left: 20px;" {% if obj.people_type == '2' %}checked{% endif %}> 医技/临床
                                <input type="radio" name="people_type" value="3" style="margin-left: 20px;" {% if obj.people_type == '3' %}checked{% endif %}> 护理
                            </td>
                        </tr>
                    </table>
                </div>
                <div style="width: 100%;">
                    <div style="width: 100%;height: 40px;line-height: 40px;background-color: #F5F5F5;color: #1E90FF;padding-left: 1%;border: #cccccc solid 1px;border-top: none;border-bottom: none;padding-right: 1%;">
                        上传附件
                        <a href="javascript:;" class="file" style="vertical-align: top;margin-top: 5px;float: right;">
                            上传文件
                            <input type="file" name="party_main_body" id="party_main_body"
                                   onchange="upload_accessory('file1', 'party_main', 'party_main_body')">
                        </a>
                    </div>
                    <table style="width: 100%;">
                        <thead>
                        <tr>
                            <td style="width: 5%;border: #cccccc solid 1px;text-align: center;">序号</td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                文件名称
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                文件大小
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                上传时间
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                上传人员
                            </td>
                            <td style="width: 15%;border: #cccccc solid 1px;border-left: none;text-align: center;">操作
                            </td>
                        </tr>
                        </thead>
                        <tbody style="overflow: auto;" id="party_main">
                        {% for record in file_list %}
                            <tr>
                                <td style="width: 5%;border: #cccccc solid 1px;text-align: center;">{{ forloop.counter }}</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.file_name }}</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.file_size }}</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.upload_time }}</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.uploader }}</td>
                                <td style="width: 15%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                    <a target='_blank' href="{{ record.file_path }}">查看</a> | <a style="color: red;"
                                                                                                 href='#'
                                                                                                 onclick='RemoveTr(this, "party_main", {{ record.id }})'>删除</a>
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block script %}
    <script type="text/javascript" src="../../static/personnel/TimeSelector/src/jedate.js"></script>
    <script>
        window.onload = function () {
            document.getElementById("loading").style.display = "none";
            document.getElementById("personage").className = "add_class";
            document.getElementById("sponsor").className = "add-nav-active";
        };

        //自定义格式选择
        jeDate("#test12",{
            format: "YYYY-MM-DD"
        });
        jeDate("#test13",{
            format: "YYYY-MM-DD"
        });

        //一次绑定多个选择
        var jel = document.querySelectorAll(".moredate");
        for(var j=0;j<jel.length;j++){
            var mat = jel[j].getAttribute("placeholder");
            jeDate(jel[j],{
                format: mat
            });
        }

        //一次绑定多个选择DIV类型
        var divel = document.querySelectorAll(".divmore");
        for(var j=0;j<divel.length;j++){
            var divmat = divel[j].getAttribute("placeholder");
            jeDate(divel[j],{
                format: divmat
            });
        }

        const uuid = "{{ obj.fj_number | safe }}"
        // 上传附件
        function upload_accessory(file_type, tbody_id, accessory_type) {
            document.getElementById("loading").style.display = "inline-block";
            var form = new FormData();
            var files = document.getElementById(accessory_type).files[0];
            form.append(file_type, files);
            form.append('number', uuid)
            form.append('source', 'qj')     // 请假
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/topics_attachments_upload", true);
            xhr.send(form);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        creat_table(JsonDate, tbody_id)
                    }
                }
            };
        }

        // 创建附件列表
        function creat_table(JsonData, tbody_id) {
            var t_table = document.getElementById(tbody_id);
            var local_host = window.location.host;
            if (Array.isArray(JsonData["pdf_path"])) {
                for (var i = 0; i < JsonData["pdf_path"].length; i++) {
                    var tr = document.createElement("tr");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");var td5 = document.createElement("td");var td6 = document.createElement("td");
                    td1.innerHTML = t_table.children.length + 1;td1.style.textAlign = "center";td1.style.border = "#cccccc solid 1px";td1.style.borderTop = "none";
                    td2.innerHTML = JsonData["file_name"];td2.style.paddingLeft = "1%";td2.style.border = "#cccccc solid 1px";td2.style.borderLeft = "none";td2.style.borderTop = "none";
                    td3.innerHTML = JsonData["file_size"];td3.style.textAlign = "center";td3.style.border = "#cccccc solid 1px";td3.style.borderLeft = "none";td3.style.borderTop = "none";
                    td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";td4.style.border = "#cccccc solid 1px";td4.style.borderLeft = "none";td4.style.borderTop = "none";
                    td5.innerHTML = JsonData["uploader"];td5.style.textAlign = "center";td5.style.border = "#cccccc solid 1px";td5.style.borderLeft = "none";td5.style.borderTop = "none";
                    td6.innerHTML = "<a target='_blank' href='" + JsonData["pdf_path"][i]["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \"" + tbody_id + "\", \"" + JsonData["pdf_path"][i]["id"] + "\")'>删除</a>";
                    td6.style.textAlign = "center";td6.style.border = "#cccccc solid 1px";td6.style.borderLeft = "none";td6.style.borderTop = "none";
                    tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
                    t_table.appendChild(tr);
                }
                if (JsonData["pdf_path"].length > 1) {
                    document.getElementById("del_adjunct").style.display = 'block';
                }
            } else {
                tr = document.createElement("tr");td1 = document.createElement("td");td2 = document.createElement("td");td3 = document.createElement("td");td4 = document.createElement("td");td5 = document.createElement("td");td6 = document.createElement("td");
                td1.innerHTML = t_table.children.length + 1;td1.style.textAlign = "center";td1.style.border = "#cccccc solid 1px";td1.style.borderTop = "none";
                td2.innerHTML = JsonData["file_name"];td2.style.paddingLeft = "1%";td2.style.border = "#cccccc solid 1px";td2.style.borderLeft = "none";td2.style.borderTop = "none";
                td3.innerHTML = JsonData["file_size"];td3.style.textAlign = "center";td3.style.border = "#cccccc solid 1px";td3.style.borderLeft = "none";td3.style.borderTop = "none";
                td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";td4.style.border = "#cccccc solid 1px";td4.style.borderLeft = "none";td4.style.borderTop = "none";
                td5.innerHTML = JsonData["uploader"];td5.style.textAlign = "center";td5.style.border = "#cccccc solid 1px";td5.style.borderLeft = "none";td5.style.borderTop = "none";
                td6.innerHTML = "<a target='_blank' href='" + JsonData["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \"" + tbody_id + "\", \"" + JsonData["id"] + "\")'>删除</a>";
                td6.style.textAlign = "center";td6.style.border = "#cccccc solid 1px";td6.style.borderLeft = "none";td6.style.borderTop = "none";
                tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
                t_table.appendChild(tr);
            }
        }

        function RemoveTr(_this, tbody_id, wj_id) {
            var table = document.getElementById(tbody_id);
            var trNode = $(_this).parent().parent();
            trNode.remove();

            for (var i = 0; i < table.children.length; i++) {
                table.children[i].children[0].innerHTML = i + 1;
            }

            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/delete_attachments_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send("id=" + String(wj_id));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        alert("删除成功！")
                    }
                }
            };
        }

        function save_data() {
            var leave_type = document.getElementById("leave_type").value;                   // 请假类型  {'value': 'a', 'leave': '年休假'}
            var reason = document.getElementById('reason').value;                       // 请假理由
            var car_start = document.getElementById('test12').value;             // 开始时间
            var car_end = document.getElementById('test13').value;                 // 结束时间
            var people_type = $("input[type=radio][name=people_type]:checked").val();
            if (!car_start) {
                alert('请输入开始时间！');
                return false
            }
            if (!car_end) {
                alert('请输入结束时间！');
                return false
            }
            document.getElementById("loading").style.display = "inline-block";
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/personal-center/leave_request_apply_draft_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send("id=" + {{ obj.id }} + "&leave_type=" + leave_type + "&reason="+ reason+"&start_time="+car_start+"&fj_number="+String(uuid)+"&end_time="+car_end+"&people_type="+people_type);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        if (JsonDate["code"] === 200) {
                            alert(JsonDate["message"])
                            window.location.href = "/personal-center/leave_request_list_view"
                        } else {
                            alert(JsonDate["message"])
                        }
                    }
                }
            };
        }

        function ExportCar() {
            var leave_type = document.getElementById("leave_type").value;                   // 请假类型  {'value': 'a', 'leave': '年休假'}
            var reason = document.getElementById('reason').value;                       // 请假理由
            var car_start = document.getElementById('test12').value;             // 开始时间
            var car_end = document.getElementById('test13').value;                 // 结束时间
            var administrative_table = document.getElementById('party_main');
            var people_type = $("input[type=radio][name=people_type]:checked").val();
            if (!car_start){
                alert("请选择开始时间！");
                return false
            }
            if (!car_end) {
                alert("请选择结束时间！");
                return false
            }
            if (!people_type) {
                alert('请选择人员类别！');
                return false
            }
            if (leave_type === 'e' || leave_type === 'f' || leave_type === 'h') {
                if (!administrative_table.children.length) {
                    alert("病假、探亲假，产假必须上传诊断证明或者父母或配偶常年在外省工作证明！");
                    return false
                }
            }
            console.log(leave_type, reason, car_start, car_end)
            document.getElementById("loading").style.display = "inline-block";
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/personal-center/leave_request_apply_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send("id=" + {{ obj.id }} + "&leave_type=" + leave_type + "&reason="+ reason+"&start_time="+car_start+"&fj_number="+String(uuid)+"&end_time="+car_end+"&people_type="+people_type);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        if (JsonDate["code"] === 200) {
                            alert(JsonDate["message"])
                            window.location.href = "/personal-center/leave_request_list_view"
                        } else {
                            alert(JsonDate["message"])
                        }
                    }
                }
            };
        }
    </script>
{% endblock %}
